package com.block.api.controller.member;

import com.alibaba.fastjson.JSONObject;
import com.block.common.annotate.NeedToken;
import com.block.common.bean.CommonParam;
import com.block.common.bean.SessionBean;
import com.block.common.exception.ApiException;
import com.block.common.model.dto.CommonReqDTO;
import com.block.common.model.dto.LoginReqDTO;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;
import com.block.db.entity.ApplicationInfo;
import com.block.db.entity.ChannelInfo;
import com.market.member.service.IBaseTypeInfoService;
import com.market.member.service.IMemberFeedbackService;
import com.market.member.service.IMemberInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.nio.channels.Channel;
import java.util.Date;

@RestController
@RequestMapping("api/member")
@Validated
public class MemberController {

    @Autowired
    IMemberInfoService memberInfoService;
    @Autowired
    IBaseTypeInfoService baseTypeInfoService;
    @Autowired
    IMemberFeedbackService memberFeedbackService;

    /**
     * 用户登陆
     */
    @RequestMapping(value = "/login", method = {RequestMethod.POST, RequestMethod.GET})
    public String login(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "appInfo") ApplicationInfo appInfo,
                             @RequestAttribute(value = "reqChannelInfo") ChannelInfo reqChannelInfo,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        String zhima = tranData.getString("zhima");
        String loginType = tranData.getString("loginType");
        String mobile = tranData.getString("mobile");
        String loginCode = tranData.getString("loginCode");// 短信验证码/一键登录token
        String channelCode = commonParam.getChannelCode();

        LoginReqDTO loginReqDTO = new LoginReqDTO();
        loginReqDTO.setLoginType(loginType);
        loginReqDTO.setMobile(mobile);
        loginReqDTO.setLoginCode(loginCode);
        loginReqDTO.setClientType(commonParam.getClientType());
        loginReqDTO.setReqIp(ip);
        loginReqDTO.setZhima(zhima);

        loginReqDTO.setAppInfo(appInfo);
        loginReqDTO.setRegisterChannel(reqChannelInfo);
        loginReqDTO.setChannelCode(channelCode);

        CommonReqDTO commonReqDTO = new CommonReqDTO();
        commonReqDTO.setReqIp(ip);
        commonReqDTO.setReqChannelCode(commonParam.getChannelCode());
        commonReqDTO.setReqChannelName(commonParam.getChannelName());

        ResponseVo responseVo = memberInfoService.toLogin(loginReqDTO, commonReqDTO, commonParam);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

    /**
     * 更新用户信息
     *  如果是首次填写用户资质信息
     *  则进行商户撞库
     */
    @NeedToken
    @RequestMapping(value = "/updateInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String updateInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "appInfo") ApplicationInfo appInfo,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        tranData.put("reqIp", ip);
        ResponseVo responseVo = memberInfoService.updateMemberInfo(sessionBean.getUid(), tranData, commonParam, appInfo);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

    /**
     * 获取用户基本信息
     *
     */
    @NeedToken
    @RequestMapping(value = "/getUserInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getUserInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = memberInfoService.getUserInfo(sessionBean.getUid(), appEncrypt.getAppId());
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

    /**
     * 用户信息采集信息配置
     * 用户必须登陆，才可以调用该接口
     */
    @NeedToken
    @RequestMapping(value = "/getBaseInfo", method = {RequestMethod.POST, RequestMethod.GET})
    public String getBaseInfo(@RequestAttribute(value = "tranData") JSONObject tranData,
                              @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                              @RequestAttribute(value = "uri") String uri,
                              @RequestAttribute(value = "startDate") Date startdate,
                              @RequestAttribute(value = "ip") String ip) throws ApiException {

        ResponseVo responseVo = baseTypeInfoService.getBaseInfo();
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }

    /**
     * 用户反馈
     *
     */
    @NeedToken
    @RequestMapping(value = "/feedback", method = {RequestMethod.POST, RequestMethod.GET})
    public String feedback(@RequestAttribute(value = "tranData") JSONObject tranData,
                              @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                              @RequestAttribute(value = "uri") String uri,
                              @RequestAttribute(value = "startDate") Date startdate,
                           @RequestAttribute(value = "commonParam") CommonParam commonParam,
                           @RequestAttribute(value = "sessionBean") SessionBean sessionBean,
                              @RequestAttribute(value = "ip") String ip) throws ApiException {

        String content = tranData.getString("content");
        String mobile = tranData.getString("mobile");

        ResponseVo responseVo = memberFeedbackService.saveInfo(sessionBean.getUid(), content, mobile, sessionBean, commonParam);
        return ApiUtil.dealReturnJson(responseVo, appEncrypt, "1.0", startdate, uri);
    }


}
